// ignore_for_file: must_be_immutable
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ton_demo/calendar/utils/enums.dart';
import 'package:ton_demo/calendar_page.dart';
import 'package:uni_links2/uni_links.dart';

class XMyApp extends StatefulWidget {
  XMyApp({super.key});

  @override
  State<XMyApp> createState() => _XMyAppState();
}

class _XMyAppState extends State<XMyApp> {
  bool a = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      initUniLinks();
    });
  }

  Future<void> initUniLinks() async {
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      Uri? uri = await getInitialUri();
      if (uri != null && uri.path == '1223') {
        /// 根据路径处理不同的逻辑
        setState(() {
          a = true;
        });
      }
    } on PlatformException {
    }
  }

  List<ViewType> viewTypeList = [
    ViewType.DAY,
    ViewType.WEEK,
    ViewType.MONTH,
    ViewType.YEAR
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(a ? '123进入' : 'Flutter Demo'),
      ),
      body: Container(
        padding: const EdgeInsets.all(10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: List.generate(
            4,
            (index) => _buildButton(viewTypeList[index], context),
          ),
        ),
      ),
    );
  }

  Widget _buildButton(ViewType viewType, BuildContext context) {
    return TextButton(
      onPressed: () => _showBottomSheet(viewType, context),
      child: Container(
        width: 100,
        height: 50,
        color: Colors.grey[200],
        margin: const EdgeInsets.all(10),
        padding: const EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(viewType.name),
          ],
        ),
      ),
    );
  }

  void _showBottomSheet(ViewType type, BuildContext context) {
    showBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return ClipRRect(
          borderRadius: BorderRadius.vertical(top: Radius.circular(16.0)),
          child: Container(
            height: MediaQuery.of(context).size.height - 30,
            color: Colors.white,
            child: Column(
              children: <Widget>[
                Expanded(
                  child: TimePage(
                    minDate: DateTime.now(),
                    maxDate: DateTime.now().add(const Duration(days: 10000)),
                    type: type,
                    getSelectedData: (DateTime begin, DateTime end) {
                      /// 确认时的回调
                      /// 除了周这俩时间都应该相等
                      print(begin.toString());
                      print(end.toString());
                    },
                    haveDataTimeList: [
                      DateTime(
                        DateTime.now().year,
                        DateTime.now().month,
                      ),
                      DateTime(
                        DateTime.now().year,
                        DateTime.now().month,
                        DateTime.now().day,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
